﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAO;
using System.ComponentModel.DataAnnotations;

namespace BLL
{
    public class UserBLL
    {
        TrainingCourseDataContext db = new TrainingCourseDataContext();
       
        public void SaveUser(User user,string pass2)
        {
            //Check Account Name
            if (string.IsNullOrEmpty(user.UserName))
            {
                throw new ArgumentNullException("Ten tai khoan khong duoc trong");
            }
            if (!IsValidEmail(user.Email))
            {
                throw new InvalidCastException("Email khong hop le!");
            }
            if (string.IsNullOrWhiteSpace(user.PassWord))
            {
                throw new ArgumentNullException("Mat khau khong duoc bo trong");
            }
            if (!(!string.IsNullOrWhiteSpace(user.PassWord)&&pass2==user.PassWord))
            {
                throw new Exception("Mat khau khong khop");
            }
            // If Pass
            try
            {
                if (IsExists(user))
                {
                    User result = db.Users.FirstOrDefault(p => p.UserName == user.UserName);
                    result.Email = user.Email;
                    result.FirstName = user.FirstName;
                    result.LastName = user.LastName;
                    result.PassWord = user.PassWord;
                    result.Status = true;
                 
                    db.SubmitChanges();
                }
                else
                {
                    user.Status = true;
                    db.Users.InsertOnSubmit(user);
                    db.SubmitChanges();
                }
            }
            catch
            {
                throw;
            }
           
        }
        /// <summary>
        /// Check mail
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        private bool IsValidEmail(string email)
        {
            var kt = new EmailAddressAttribute();
            return kt.IsValid(email);
            
            
        }

        /// <summary>
        /// Kiem tra ton tai
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// 
        public bool IsExists(User user)
        {
            bool flag = false;
            if (db.Users.FirstOrDefault(p => p.UserName == user.UserName) != null)
                flag = true;
            return flag ;
        }
        public void OffUser(string userName)
        {
            User user = db.Users.FirstOrDefault(p => p.UserName == userName);          
           
                user.Status = false;
            try
            {
                db.SubmitChanges();
            }
            catch
            {

            }
           
                
            
            
        }
        public List<User> GetUserOnline()
        {
            List<User> lsUser = db.Users.Where(p => p.Status == true).ToList();
            return lsUser;
        }

    }
}
